Ext.define('GBIF.MainViewport', {
		extend: 'Ext.container.Viewport'	
	,	alias: 'widget.mainViewport'
	,	border: false
	,	layout: 'border'
	,	id: 'mainViewID'
	,	listeners: {
				render: function(){
					this.showMainWindow();
				}
		}
	,	initComponent: function() {
			this.maintbar = Ext.create('GBIF.MainToolbar', {});
			this.viewCard = Ext.create('GBIF.ViewCard', {
					region: 'center'
				,	tbar: this.maintbar
			});
			this.viewCard.overviewPanel.on('archivingStatus', function(me){
				var currentActive = this.viewCard.getLayout().getActiveItem().currentActive;
				if(currentActive != 'overview'){
					this.viewCard.changeLayout(0);
				}
				this.maintbar.statusEnableDisable();
			}, this);
			this.viewCard.overviewPanel.on('statusChanged', function(me){
				this.maintbar.statusEnableDisable();
				if(GBIF.currentStatus == 'LOADED' || GBIF.currentStatus == 'CLASSIFIED'){
					Ext.fly(Ext.select('div#viewData').elements[0]).on('click', function(){
						this.viewCard.changeLayout(1);
					}, this);
				}
				if(GBIF.currentStatus == 'CLASSIFIED'){
					Ext.fly(Ext.select('div#GenrateDWCA').elements[0]).on('click', function(){
						this.viewCard.genrateDWCA();
					}, this);
				}
			}, this);
			
			this.viewCard.reclassifiyGrid.on('startClassification', function(status, me){
				var currentActive = this.viewCard.getLayout().getActiveItem().currentActive;
				if(currentActive != 'overview'){
					this.viewCard.changeLayout(0);
				}
				GBIF.currentStatus = status;
				this.maintbar.statusEnableDisable();
				this.viewCard.overviewPanel.updateTpl(GBIF.currentStatus);
			}, this);
			
			this.maintbar.on('changeCard', function(id, me){
					this.viewCard.changeLayout(id);
			}, this);

			this.viewCard.on('layoutChanged', function(id, me){
				var menuItem = this.maintbar.viewBtn.menu.items.items[id];
				if(!menuItem.checked){
					menuItem.setChecked(true);
				}
			},this);

			this.maintbar.closeProject.on('click', function(me){
					Ext.Msg.confirm(this.msgNoticeTitle, this.msgCloseProject, function(btn){
						if(btn == 'yes'){
							this.showMainWindow();
							this.viewCard.changeLayout(0);
							this.viewCard.reclassifiyGrid.store.removeAll();
							this.viewCard.overviewPanel.resetAll();
						}
					}, this);
			}, this);

			this.header = Ext.create('Ext.container.Container', {
					height: 95
				,	cls: 'glossary-header'		
				,	region: 'north'
				,	border: false
				,	margins: '0 0 5 0'					
				,	html: '<div class="header">'+
					 '<a href="http://www.gbif.org/" target="_blank" class="gbif">'+
					 '<img src="resources/images/gbif.jpg" alt="GBIF" width="104" height="90" border="0" /></a>'+
					 '<div class="header-text">'+
						 '<h1>Darwin Core Archive - Classification Service</h1>'+
						 '<p><i>A tool for annotating occurrence data with taxonomic authority files</i></p>'+
					 '</div></div>'
			});		
							
			this.items = [ this.header, this.viewCard ]
			this.callParent();
		}
	,	showMainWindow: function(){
			var pwindow = Ext.create('GBIF.ProjectWindow',{}).show();
			pwindow.pidForm.getForm().findField('projectCode').setValue(GBIF.tmpProjectID);
			pwindow.on('close', function(me){
					GBIF.currentProjectID = me.getProjectID();
					GBIF.currentStatus = me.getStatus();
					this.maintbar.setProjectName(GBIF.currentProjectID);
					this.maintbar.statusEnableDisable();
					this.viewCard.overviewPanel.showHideTbar(GBIF.currentStatus);
					this.viewCard.overviewPanel.updateTpl(GBIF.currentStatus);
			}, this);
			GBIF.currentProjectID = '';
			GBIF.currentArchiveUrl = '';
			GBIF.currentErrorCode = '';
		}
});